<?php
/**
 * 系统部门管理
 *
 * @author maomao.thx@gmail.com
 * @copyright Copyright (c) 2009 Green Shore (http://www.iwgame.com)
 * @version $Id: DepartmentController.php 1059 2011-11-18 09:26:15Z maomao.thx@gmail.com $
 */
class System_DepartmentController extends IG_Controller_Action
{
    /**
     * 部门列表
     * @author maomao.thx@gmail.com
     */
    public function listAction()
    {
        $this->setNoRender();
        $report = IG_Data::factory('array', array(
            'source'  => array(
                'name'   => 'Model',
                'config' => array(
                    'model' => 'System_Model_Department',
                ),
            ),
            'plugins' => array(
                array(
                    'name'   => 'DwzTable',
                    'config' => array(
                        'columns' => array(
                            'master'  => '负责人',
                            'name'    => '部门名称',
                            'intro'   => '部门介绍',
                        ),
                        'attrs' => array('layoutH' => 137),
                    )
                ),
                array(
                    'name'   => 'Params',
                    'config' => array(
                        'name'          => array(
                            'title' => '部门名称',
                            'type'  => 'formText',
                            'default'   => '',
                        ),
                    ),
                ),
                array(
                    'name'   => 'DwzPanelBar',
                    'config' => array(
                        'buttons' => array(
                            '添加' => 'class="add" href="/system/department/save/" ' .
                                      'target="navTab" title="添加部门"',
                            '编辑' => 'class="edit" href="/system/department/save/id/{department_id}" ' .
                                      'target="navTab" ' .
                                      'title="编辑部门"',
                            '删除' => 'class="delete" ' .
                                      'href="/system/department/delete/id/{department_id}" ' .
                                      'target="ajaxTodo" title="确定要删除吗?" ' .
                                      'callback="IG.data.delete"',
                        ),
                    ),
                ),
            ),
        ));

        $report->loadPlugins();
    }

    /**
     * 部门增加，修改
     * @author maomao.thx@gmail.com
     */
    public function saveAction ()
    {
        $model = $this->getModel();

        if ($this->getRequest()->isPost()) {
            $params = $this->_getParams(
                        array('department_id',
                              'name',
                              'master',
                              'intro'
            ));
            try {
                $result = $model->setOptions($params)->save();
            } catch (Exception $e) {
                if ($e->getCode() == '23000') {
                    $this->_helper->json(array(
                        'statusCode' => 1,
                        'message'    => "部门名称已存在"
                    ));
                } else {
                    throw new IG_Exception($e->getMessage());
                }
            }

            $this->_helper->json(array(
                'statusCode' => 0,
                'message'    => "",
                'callbackType' => 'closeCurrent',
                'navTabId'     => '/system/department/list'
            ));
        }

        if ($id = $this->_getParam('id')) {
            $this->view->assign('detail', $model->find($id));
        } else {
            $this->view->assign('detail', $model);
        }
    }

    /**
     * 部门删除
     * @author maomao.thx@gmail.com
     */
    public function deleteAction()
    {
        if ($this->getRequest()->isPost()) {
            $id = $this->_getParam('id');
            $model = $this->getModel();
            $result = $model->delete($id);

            $this->_helper->json(array(
                'statusCode' => (int) ($result === false),
                'message'    => "操作成功!"
            ));
        }
        exit;
    }

    protected $_model;

    public function getModel()
    {
        if (!$this->_model) {
            $this->_model = new System_Model_Department();
        }
        return $this->_model;
    }
}
